﻿using System;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.EventAggregators;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.TicketManagement.Models;

namespace IntoNails.Modules.TicketManagement.ViewModels
{
    public partial class TicketWorkspaceViewModel
    {
        private void OnCreateNewTicketEventSubscribed(Ticket ticket)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            TicketDataContext dataContext = new TicketDataContext(ticket.Status, false);

            this.ShowTicketInteractionDialog.Raise(
                ticket,
                dataContext,
                this.CreateNewTicketCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true)
                );
        }

        private void CreateNewTicketCallback(Ticket ticketCallback)
        {
            // TODO: check input
            this._ticketRepository.Add(ticketCallback);
            this._ticketRepository.Save();

            // add new user to list
            this.TicketCollection.Add(ticketCallback);

            // unlock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }

        private void OnEditTicketEventSubscribed(object parameter)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedTicket == null)
            {
                MessageBoxContext context = new MessageBoxContext("Please select a Ticket to edit first.",
                                                                  GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                                                                  MessageBoxIcons.Exclamation);
                this.ShowMessageBox.Raise(context,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            TicketDataContext dataContext = new TicketDataContext(this.SelectedTicket.Status, true);

            this.ShowTicketInteractionDialog.Raise(
                this.SelectedTicket,
                dataContext,
                this.EditTicketCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void EditTicketCallback(Ticket ticketCallback)
        {
            if (ticketCallback == null)
            {
                throw new InvalidOperationException("ticketCallback");
            }

            ticketCallback.UpdatedDate = DateTime.Now;

            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);

            // TODO: check input
            this._ticketRepository.Save();
        }

        private void OnDeleteTicketEventSubscribed(MessageBoxContext context)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedTicket == null)
            {
                MessageBoxContext warningMsgBoxContext = new MessageBoxContext(
                    "Please select a Ticket to edit first.",
                    GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                    MessageBoxIcons.Exclamation);

                this.ShowMessageBox.Raise(warningMsgBoxContext,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            this.ShowMessageBox.Raise(context, this.MessageBoxSubmitCallback,
                                      () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void MessageBoxSubmitCallback(MessageBoxContext confirmedDeleteMsgBoxCallback)
        {
            // update data-storage
            this._ticketRepository.Remove(this.SelectedTicket.UniqueCode);
            this._ticketRepository.Save();

            // update UI
            this.TicketCollection.Remove(this.SelectedTicket);

            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }
    }
}
